'use strict'

const { questionnaire, questionnaireAnswers } = require('../models')

// 创建调查问卷视图
var fn_publishQS = async (ctx, next) => {
    // 获取用户名
    var username = ctx.session.userName;

    var obj = {
        // 用户
        username: username
    };

    ctx.render("publishQS.html",obj)
};

// 创建调查问卷判断
var fn_publishQSJudge = async (ctx, next) => {
    // 1.获取前端返回的题目，2.储存进数据库

    // 发表调查问卷人Id
    var userNameId = ctx.session.userId;

    // 调查问卷名
    var QSTheme = ctx.request.body.QSTheme.trim();
    // 调查问卷简介
    var qsSynopsis = ctx.request.body.qsSynopsis.trim();
    //  题目
    var Question1Box = ctx.request.body.Question1Box.trim();
    var Question2Box = ctx.request.body.Question2Box.trim();
    var Question3Box = ctx.request.body.Question3Box.trim();
    var Question4Box = ctx.request.body.Question4Box.trim();
    var Question5Box = ctx.request.body.Question5Box.trim();
    var Question6Box = ctx.request.body.Question6Box.trim();
    var Question7Box = ctx.request.body.Question7Box.trim();
    var Question8Box = ctx.request.body.Question8Box.trim();
    var Question9Box = ctx.request.body.Question9Box.trim();
    var Question10Box = ctx.request.body.Question10Box.trim();
    // 测试是否成功获取
    // console.log(userNameId);
    // console.log(QSTheme);
    // console.log(Question1Box);

    // 向questionnaire数据表存题目
    var questionnaires = await questionnaire.create({
        // 发表调查问卷人Id
        publishUserId: userNameId,
        // 调查问卷名
        qsName: QSTheme,
        // 调查问卷名
        qsSynopsis: qsSynopsis,
        //  题目
        topicOne: Question1Box,
        topicTwo: Question2Box,
        topicThree: Question3Box,
        topicFour: Question4Box,
        topicFive: Question5Box,
        topicSix: Question6Box,
        topicSeven: Question7Box,
        topicEight: Question8Box,
        topicNine: Question9Box,
        topicTen: Question10Box
    });

    let res = "";
    res = { code: 200, msg: "发表调查问卷成功" };
    ctx.body = JSON.stringify(res);



};

var fn_publishQSOk = async (ctx, next) => {
    //提交成功
    var obj = {
        // 网页头像
        icon: "/statics/img/QSOkicon.png",
        // 网页插图
        inbetweening: "/statics/img/QSOk.png",
        // 网页标题
        title: "发布调查问卷成功",
        // 错误内容
        OkContent: "恭喜你参加调查问卷已完成！",
        // 按钮一路径
        router1: '/home/publishQS',
        // 按钮一文本
        button1: "再发表一份",
        // 按钮二路径
        router2: '/home',
        // 按钮二文本
        button2: "返回主页面"
    }
    ctx.render('QSOk.html', obj)
};

module.exports = {
    '/home/publishQS': ['get', fn_publishQS],
    '/home/publishQSOk': ['get', fn_publishQSOk],
    '/home/publishQSJudge': ['post', fn_publishQSJudge]
}